package com.cu.employmentmanagementsystem.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import com.cu.employmentmanagementsystem.common.exception.CommonException;
import com.cu.employmentmanagementsystem.dao.StudentRecruitmentDao;
import com.cu.employmentmanagementsystem.entity.RecruitInformation;
import com.cu.employmentmanagementsystem.entity.StudentVita;
import com.cu.employmentmanagementsystem.entity.User;
import com.cu.employmentmanagementsystem.service.RecruitInformationService;
import com.cu.employmentmanagementsystem.service.StudentVitaService;
import com.cu.employmentmanagementsystem.util.CommonUtils;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import com.cu.employmentmanagementsystem.common.result.R;
import com.cu.employmentmanagementsystem.entity.StudentRecruitment;
import com.cu.employmentmanagementsystem.service.UserService;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * (RecruitInformation)表控制层
 *
 * @author makejava
 * @since 2024-07-02 16:20:13
 */
@RestController
@RequestMapping("recruit")
@SaCheckLogin
public class RecruitInformationController {
    /**
     * 服务对象
     */
    @Resource
    private RecruitInformationService recruitInformationService;

    //因为service层的insert返回值是对象，不是int，为了判断方便，这里用dao
    @Autowired
    private StudentRecruitmentDao studentRecruitmentDao;

    @Autowired
    private UserService userService;

    @Autowired
    private StudentVitaService studentVitaService;

    /**
     * 查看审核通过的公司的招聘信息
     * @return 查询结果
     */
    @RequestMapping("queryRecruitments")
    @ApiOperation(value = "查询招聘信息", notes = "根据审核状态查询招聘信息")
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询招聘信息成功！"),
            @ApiResponse(code = 401, message = "用户未登录！"),
            @ApiResponse(code = 500, message = "查询招聘信息失败！")
    })
    public R<List<RecruitInformation>> queryRecruitments() {
        String status = "1";
        try{
            List<RecruitInformation> list = recruitInformationService.queryByStatus(status);
            return R.success(list);
        }catch (Exception e){
            throw new CommonException("查询招聘信息的过程中出现异常");
        }
    }

    /**
     * 选择指定的招聘信息进行投递简历
     * @param recruitId 筛选条件
     * @return 查询结果
     */
    @RequestMapping("commitVita")
    @ApiOperation(value = "投递简历")
    public R<List<RecruitInformation>> commitVita(@ApiParam(name="recruitId",value="此时的招聘表已经是审核通过的") Long recruitId) {
        try{
            //获取当前用户id
            Long studentID = CommonUtils.getStudentId();
            //根据用户id得到唯一简历
            StudentVita s = studentVitaService.selectByStudentId(studentID);
            if(s.getPeviewStatus() != 1)
            {
                throw new CommonException("您没有可用的简历！");
            }
            //构造简历__招聘信息对象并赋值
            StudentRecruitment studentRecruitment = new StudentRecruitment();
            studentRecruitment.setRecruitmentId(recruitId);
            studentRecruitment.setResumeId(s.getId());
            boolean isExist = studentRecruitmentDao.count(studentRecruitment) > 0;
            if(isExist){
                throw new CommonException("您已经投递过该招聘岗位");
            }
            studentRecruitment.setDeliveryTime(LocalDateTime.now());
            studentRecruitment.setDeliveryStatus(-1);
            //投递简历，即将新建的学生招聘信息插入数据库
            int x = studentRecruitmentDao.insert(studentRecruitment);
            if( x == 1){
                return R.success(recruitInformationService.queryByStatus("投递成功"));
            }
            else{
                return R.error("投递失败");
            }
        }catch (Exception e){
            throw e;
        }
    }

    //发布招聘信息
    @RequestMapping("commitRecruit")
    @ApiOperation(value = "发布招聘信息")
    @ApiResponses({
            @ApiResponse(code = 200, message = "招聘信息发布成功！"),
            @ApiResponse(code = 401, message = "用户未登录！"),
            @ApiResponse(code = 500, message = "招聘信息发布失败！")
    })
    public R commitRecruit(@RequestBody @ApiParam(name = "recruitInformation", value = "招聘信息")RecruitInformation recruitInformation){
        try{
           return R.success(recruitInformationService.commitRecruit(recruitInformation));
        }catch(Exception e){
            throw new CommonException("发布招聘信息的过程中出现异常！");
        }
    }


    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<RecruitInformation> queryById(@PathVariable("id") Long id) {
        return ResponseEntity.ok(this.recruitInformationService.queryById(id));
    }


    /**
     * 更新招聘信息
     *
     * @param recruitInformation 实体
     * @return 更新招聘信息
     */
    @RequestMapping("edit")
    @ApiOperation("更新招聘信息")
    @ApiParam(value = "招聘信息实体", required = true)
    public R<Boolean> edit(@RequestBody RecruitInformation recruitInformation) {
        return R.success(this.recruitInformationService.update(recruitInformation));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @RequestMapping("delete")
    @ApiOperation("根据ID删除招聘信息")
    @ApiImplicitParam(name = "id", value = "招聘信息ID", required = true, dataType = "Long", paramType = "path")
    public R<Boolean> deleteById(Long id) {
        return R.success(this.recruitInformationService.deleteById(id));
    }

}